package com.guanjia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guanjia.entity.*;
import com.guanjia.entity.vo.Image;
import com.guanjia.entity.vo.ImageCorr;
import com.guanjia.entity.vo.InvestInfoVo;
import com.guanjia.mapper.*;
import com.guanjia.service.InvestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class InvestServiceImpl implements InvestService {
    public static Integer CURRENT_PAGE = 1;
    public static Integer PAGE_SIZE = 5;

    @Autowired
    private CreditinfoMapper creditinfoMapper;

    @Autowired
    private InvestMapper investMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BussinessMapper bussinessMapper;

    @Autowired
    private RepaymentMapper repaymentMapper;

    @Override
    @Transactional
    public void saveOrder(Integer investid, Integer userId, Integer crowd_user_id, Double money) {
        double ba;
        CreditInformation creditInformation=creditinfoMapper.findCreditInfoById(investid);
        double sc= money/creditInformation.getBorrowmoney()*100;
        if(sc+Double.valueOf(creditInformation.getSchedule())==100){
            creditInformation.setStep("2");

            Calendar calendar = new GregorianCalendar();
            calendar.setTime(new Date());
            System.out.println(calendar.getTime());
            for (int i =0;i<Integer.parseInt(creditInformation.getBorrowtime());i++){
                Repayment repayment = new Repayment();
                repayment.setUserId(userId);
                repayment.setCreditId(creditInformation.getId());
                repayment.setRepaymentmoney(creditInformation.getBorrowmoney());
                calendar.add(calendar.MONTH,1);
                repayment.setPepaymenttime(calendar.getTime());
                repayment.setStatus("0");
                repayment.setAlready(0.0);
                repayment.setNotreturned(creditInformation.getMonthlyReturn());
                repaymentMapper.inerstByRepayment(repayment);
            }

        }

        String nowSc= String.valueOf((double)Math.round((sc+Double.valueOf(creditInformation.getSchedule()))*100/100));

        creditInformation.setSchedule(nowSc);
        creditinfoMapper.updateById(creditInformation);
        //计算月收款金额
        Double investmoney=money/creditInformation.getBorrowmoney()*creditInformation.getMonthlyReturn();
        investmoney=  (double) Math.round(investmoney* 100) / 100;
        Invest invest=new Invest();
        invest.setCreditId(investid);
        invest.setInvestmoney(money);
        invest.setUserId(userId);
        invest.setInterest(investmoney);
        invest.setCreatetime(new Date());

        investMapper.saveInvest(invest);
        User user=userMapper.findUserById(userId);
        ba=user.getBalance();
        user.setBalance(user.getBalance()-money);
        userMapper.updateById(user);
        Bussiness bussiness=new Bussiness();//支出信息
        bussiness.setUserId(userId);
        bussiness.setBalanceout(Double.valueOf(money));
        bussiness.setType("2");
        bussiness.setBalance(ba-Double.valueOf(money));
        bussinessMapper.insert(bussiness);

        User crowd_user=userMapper.findUserById(crowd_user_id);//收入
        crowd_user.setBalance(crowd_user.getBalance()+money);
        ba=crowd_user.getBalance();
        userMapper.updateById(crowd_user);
        Bussiness corwdBuss=new Bussiness();//支出信息
        corwdBuss.setUserId(crowd_user_id);
        corwdBuss.setBalancein(Double.valueOf(money));
        corwdBuss.setType("3");
        corwdBuss.setBalance(ba+Double.valueOf(money));
        bussinessMapper.insert(corwdBuss);



    }

    @Override
    public IPage<CreditInformation> findList(Integer page, Integer size,String sort,String pname,Integer max,Integer min) {
        if (page != null) {
            CURRENT_PAGE = page;
        }
        if (size != null) {
            PAGE_SIZE = size;
        }
        QueryWrapper<CreditInformation> queryWrapper=new QueryWrapper<CreditInformation>().ne("step",0);
        if(sort!=null&&sort.equals("1")){
        queryWrapper =  new QueryWrapper<CreditInformation>().orderByDesc("rateofreturn").ne("step",0);
        }
        if(sort!=null&&sort.equals("2")){
            queryWrapper =  new QueryWrapper<CreditInformation>().orderByAsc("rateofreturn").ne("step",0);
        }

        if(pname!=null){
            queryWrapper =  new QueryWrapper<CreditInformation>().like("projectname",pname).ne("step",0);
        }
        if (max!=null||min!=null){
            queryWrapper =  new QueryWrapper<CreditInformation>().between("borrowmoney",min,max).ne("step",0);
        }
        IPage<CreditInformation> list = creditinfoMapper.selectPage(
                new Page<>(CURRENT_PAGE, PAGE_SIZE),
                queryWrapper
                );
        return list;
    }

    @Override
    public CreditInformation findInfoById(Integer id) {
        return investMapper.findInfoById(id);
    }

    @Override
    public List<InvestInfoVo> findById(Integer id) {
        return investMapper.findById(id);
    }

    /*
    * 显示图片
    * */

    @Override
    public List<Image> FindCarList(Integer id) {
        return investMapper.FindCarList(id);
    }

    @Override
    public List<Image> FindHomeList(Integer id) {
        return investMapper.FindHomeList(id);
    }

    @Override
    public List<Image> FindIdCardList(Integer id) {
        return investMapper.FindIdCardList(id);
    }

    @Override
    public List<Image> FindOtherList(Integer id) {
        return investMapper.FindOtherList(id);
    }


}
